package com.luwanglin.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luwanglin.commonutils.R;
import com.luwanglin.eduservice.entity.EduCourse;
import com.luwanglin.eduservice.entity.vo.CourseInfoVo;
import com.luwanglin.eduservice.entity.vo.CoursePublishVo;
import com.luwanglin.eduservice.entity.vo.CourseQuery;
import com.luwanglin.eduservice.service.EduCourseService;
import com.luwanglin.commonutils.exception.GuliException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    final
    EduCourseService courseService;

    public EduCourseController(EduCourseService courseService) {
        this.courseService = courseService;
    }

    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String courseId = courseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId", courseId);
    }


    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable("courseId") String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo", courseInfoVo);
    }

    @PutMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfo) {
        courseService.updateCourseInfo(courseInfo);
        return R.ok();
    }

    @GetMapping("getPublishCourseInfo/{id}")
    public R getCoursePublishInfo(@PathVariable("id") String courseId) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVO(courseId);
        if (coursePublishVo == null) {
            throw new GuliException(20001, "no this data");
        }
        return R.ok().data("item", coursePublishVo);
    }

    @PutMapping("publishCourse/{id}")
    public R publicCourse(@PathVariable("id") String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus(EduCourse.COURSE_NORMAL);
        courseService.updateById(eduCourse);
        return R.ok();
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象")
                    CourseQuery courseQuery) {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {

        boolean result = courseService.removeCourseById(id);
        if (result) {
            return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }

}

